import { isEmpty, isUsername, isMobile, isEmail } from '@/utils/index'

/**
 * 验证中文用户名
 *
 * @param rule
 * @param value
 * @param callback
 */
export const validateChineseUsername = (rule: string, value: string, callback: any) => {
  const regExp = /^[\u4E00-\u9FA5]{2,4}$/
  if (!regExp.test(value)) {
    callback(new Error('必须填写中文'))
  } else {
    callback()
  }
}

/**
 * 验证用户名
 *
 * @param rule
 * @param value
 * @param callback
 */
export const validateUsername = (rule: string, value: string, callback: any) => {
  // const regExp = /^[a-zA-Z][a-zA-Z0-9_]{4,14}$/
  // if (!regExp.test(value)) {
  if (!isEmpty(value) && !isUsername(value)) {
    callback(new Error('字母开头，允许5-15字节，允许字母数字下划线'))
  } else {
    callback()
  }
}

/**
 * 验证手机号码
 *
 * @param rule
 * @param value
 * @param callback
 */
export const validateMobile = (rule: string, value: string, callback: any) => {
  // const regExp = /^1[3-9][0-9]\d{4,8}$/
  // if (!regExp.test(value)) {
  if (!isEmpty(value) && !isMobile(value)) {
    callback(new Error('手机号码不正确'))
  } else {
    callback()
  }
}

/**
 * 验证邮箱
 *
 * @param rule
 * @param value
 * @param callback
 */
export const validateEmail = (rule: string, value: string, callback: any) => {
  if (!isEmpty(value) && !isEmail(value)) {
    callback(new Error('邮箱不正确'))
  } else {
    callback()
  }
}
